﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Data;

namespace ReadExcel
{
    /// <summary>
    /// 
    /// </summary>
    class ExcelFactory
    {
        /// <summary>
        /// Gets or sets the path.
        /// </summary>
        /// <value>
        /// The path.
        /// </value>
        private string Path {get; set; }

        /// <summary>
        /// Gets or sets the data set.
        /// </summary>
        /// <value>
        /// The data set.
        /// </value>
        private DataSet dataSet { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="s">The s.</param>
        public delegate void Callback(string s);

        /// <summary>
        /// Occurs when [notify].
        /// </summary>
        public event Callback Notify;

        /// <summary>
        /// Gets or sets the state of the subject.
        /// </summary>
        /// <value>
        /// The state of the subject.
        /// </value>
        public string SubjectState { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExcelFactory"/> class.
        /// </summary>
        /// <param name="path">The path.</param>
        public ExcelFactory(string path)
        {
            this.Path = path;
        }

        /// <summary>
        /// Creates the table columns.
        /// </summary>
        /// <param name="dataTable">The data table.</param>
        /// <param name="tableColumns">The table columns.</param>
        private void CreateTableColumns(System.Data.DataTable dataTable, List<string> tableColumns)
        {
            List<string> columnsNames = new List<string>();
            foreach (string columnName in columnsNames)
            {
                dataTable.Columns.Add(columnName);
            }
        }

        /// <summary>
        /// Goes this instance.
        /// </summary>
        public void Go()
        {
            foreach (string s in ReadExcel())
            {
                SubjectState = s;
                Notify(s);
            }
        }

        /// <summary>
        /// Reads the excel.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<string> ReadExcel()
        {
            Application application = new Application();
            Workbook workbook = application.Workbooks.Open(this.Path);
            foreach (_Worksheet xlWorksheet in workbook.Sheets)
            {
                if (!string.Equals(xlWorksheet.Name, "Table Index"))
                {
                    ////Range xlRange = xlWorksheet.UsedRange;

                    ////foreach (Range row in xlRange.Rows)
                    ////{
                    ////    foreach (Range col in row.Cells)
                    ////    {
                    ////        if (col != null && col.Value2 != null)
                    ////        {
                    ////            Console.Write(col.Value2.ToString());
                    ////        }
                    ////    }
                    ////}
                    yield return xlWorksheet.Name; 
                }
            }
        }
    }
}
